package com.wz.jiangsu.main.test;

import java.util.concurrent.atomic.AtomicInteger;

public class MyCasTestDemo {
 
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
 
    private static int num = 0;
 
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 1 ;i <=10000;i++){
                    // 开启线程后，主线程和子线程分别对num++进行测试，理论结果应该为200
                    num++;
                    atomicInteger.incrementAndGet();
                }
            }
        };
        t1.setName("子线程");
        t1.start();
        for (int i = 1;i<=10000;i++){
            num++;
            atomicInteger.incrementAndGet();
        }
        System.out.println("实际num的值为："+num); // 与理论结果不一致
        System.out.println("采用CAS的方式的值为："+atomicInteger.get());// 20000
    }
}